// BringToFront.java
// by Alexandre Martins, 2013
// MAC 5857 - Prof. Leonidas

package example.ilm.model;

import ilm.framework.assignment.model.DomainAction;
import ilm.framework.domain.DomainModel;

import java.util.List;
import java.util.ArrayList;

public class BringToFront extends DomainAction {

    private IlmDomainModel _domain;
    private GeometricElement geometricElement;
    private Workspace workspace;
    private List oldOrdering;
    private static int cnt = 0;

    public BringToFront() {
        super("bringToFront[" + (++cnt) + "]", "brought a geometric element to the front");
        oldOrdering = new ArrayList();
        geometricElement = null;
        workspace = null;
    }

    public void setGeometricElement(GeometricElement geomel) {
        geometricElement = geomel;
    }

    public void setWorkspace(Workspace worksp) {
        workspace = worksp;
    }

    /**
     * Defines the DomainModel on which this action can happen. It must be
     * converted to IlmDomainModel so this class can call its specific methods.
     * 
     * @param an
     *            IlmDomainModel
     */
    public void setDomainModel(DomainModel model) {
        _domain = (IlmDomainModel) model;
    }

    /**
     * Method that is called when this action is executed.
     */
    protected void executeAction() {
        oldOrdering = new ArrayList(workspace.getGeometricElementList());
        _domain.bringToFront(_currentState, workspace, geometricElement);
    }

    /**
     * Method that is called when this action is undone.
     */
    protected void undoAction() {
    	_domain.setWorkspaceGeometricElementList(_currentState, workspace, oldOrdering);
    }

    /**
     * Compares this to another DomainAction.
     */
    public boolean equals(DomainAction a) {
        BringToFront b = (BringToFront)a;
        return (getName().equals(a.getName()) && getDescription().equals(a.getDescription()) && geometricElement.equals(b.geometricElement)) && oldOrdering.equals(b.oldOrdering) && workspace == b.workspace;
    }

    /**
     * This method is used for converting this object into string
     * 
     * @see example.ilm.model.IlmDomainConverter
     */
    public String toString() {
        return "" + 
            "<bringtofront>" + 
                "<name>" + getName() + "</name>" + 
                "<description>" + getDescription() + "</description>" + 
                "<geometricelement>" + 
                    geometricElement.toString() + 
                "</geometricelement>" + 
            "</bringtofront>";
    }
}
